import axios from "axios"
import {userStore} from "@/stores/user.ts"

const instance1=axios.create({
    baseURL:import.meta.env.VITE_BASE
})

//获取到token

function getToken(){
    const store=userStore()
    let token=""
    //如果变量对象不空的话  取到token
    if(Object.keys(store.user).length!=0){
        token=store.user.token;
    }
    return token
}

instance1.interceptors.request.use(
    (config)=>{
        config.headers.Authorization=getToken()
        return config
    },
    (err)=>{
        return err
    }
)

instance1.interceptors.response.use(
    (result)=>{
        if(result.data.meta.status==401){
            ElMessage.error('token过期请登录');
            //清楚token 并且跳转到登录页面
        }
        return result
    },
    (err)=>{
        return err
    }
)

const instance2=axios.create({
    baseURL:import.meta.env.VITE_URL
})


export {instance1,instance2}